﻿////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// <copyright>Copyright 2008-2011 Andreas Huber Doenni</copyright>
// Distributed under the Microsoft Public License (Ms-PL).
// See accompanying file License.txt or copy at http://mews.codeplex.com/license.
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

namespace Mews.Application
{
    using System;
    using System.Windows.Forms;

    using Mews.Application.Properties;
    using Mews.Net.Nntp.Cache;

    /// <summary>The form that displays the properties of a host.</summary>
    internal sealed partial class GroupForm : DoneCancelForm
    {
        private const decimal MinArticleLimit = 1;
        private const decimal MaxArticleLimit = 10000;

        private readonly IHost host;

        ////////////////////////////////////////////////////////////////////////////////////////////////////////////////

        internal GroupForm(Form owner, IHost host, int groupNameMaxLength, int groupDisplayNameMaxLength) :
            this(owner, groupNameMaxLength, groupDisplayNameMaxLength, false)
        {
            this.host = host;
        }

        internal GroupForm(
            Form owner,
            string groupName,
            int groupNameMaxLength,
            string groupDisplayName,
            int groupDisplayNameMaxLength,
            int newInboundArticlesReceiveLimit,
            int cachedInboundArticlesLimit) :
            this(owner, groupNameMaxLength, groupDisplayNameMaxLength, false)
        {
            this.groupNameTextBox.Text = groupName;
            this.groupNameTextBox.Enabled = false;
            this.selectGroupButton.Enabled = false;
            this.groupDisplayNameTextBox.Text = groupDisplayName;
            this.newInboundArticlesReceiveLimitNumericUpDown.Value = newInboundArticlesReceiveLimit;
            this.cachedInboundArticlesLimitNumericUpDown.Value = cachedInboundArticlesLimit;
        }

        internal string GroupName
        {
            get { return this.groupNameTextBox.Text; }
        }

        internal string GroupDisplayName
        {
            get { return this.groupDisplayNameTextBox.Text; }
        }

        internal int NewInboundArticlesReceiveLimit
        {
            get { return (int)this.newInboundArticlesReceiveLimitNumericUpDown.Value; }
        }

        internal int CachedInboundArticlesLimit
        {
            get { return (int)this.cachedInboundArticlesLimitNumericUpDown.Value; }
        }

        ////////////////////////////////////////////////////////////////////////////////////////////////////////////////

        private GroupForm(Form owner, int groupNameMaxLength, int groupDisplayNameMaxLength, bool hideCancelButton) :
            base(owner, Resources.GroupProperties, false, false, hideCancelButton)
        {
            this.InitializeComponent();
            this.groupNameTextBox.MaxLength = groupNameMaxLength;
            this.groupDisplayNameTextBox.MaxLength = groupDisplayNameMaxLength;
        }

        private void OnSelectGroupButton_Click(object sender, EventArgs e)
        {
            using (var form = new SelectGroupForm(this.host))
            {
                if (form.ShowDialog() == DialogResult.OK)
                {
                    this.groupNameTextBox.Text = form.SelectedGroup;
                }
            }
        }

        private void OnNewInboundArticlesReceiveLimitNumericUpDown_ValueChanged(object sender, System.EventArgs e)
        {
            this.newInboundArticlesReceiveLimitNumericUpDown.Value =
                ConstrainToBounds(this.newInboundArticlesReceiveLimitNumericUpDown.Value);
            this.cachedInboundArticlesLimitNumericUpDown.Value = Math.Max(
                this.newInboundArticlesReceiveLimitNumericUpDown.Value,
                this.cachedInboundArticlesLimitNumericUpDown.Value);
        }

        private void OnCachedInboundArticlesLimitNumericUpDown_ValueChanged(object sender, System.EventArgs e)
        {
            this.cachedInboundArticlesLimitNumericUpDown.Value =
                ConstrainToBounds(this.cachedInboundArticlesLimitNumericUpDown.Value);
            this.newInboundArticlesReceiveLimitNumericUpDown.Value =
                Math.Min(this.cachedInboundArticlesLimitNumericUpDown.Value, this.newInboundArticlesReceiveLimitNumericUpDown.Value);
        }

        private static decimal ConstrainToBounds(decimal articleCount)
        {
            // We're deliberately implementing this functionality ourselves instead of using the Maximum & Minimum
            // properties. The ValueChanged handlers are *not* called when a value outside of the range is entered!
            return Math.Max(Math.Min(articleCount, MaxArticleLimit), MinArticleLimit);
        }
    }
}
